//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  ChangePasswordBoard.m
//  iHeima
//
//  Created by he chao on 6/9/14.
//  Copyright (c) 2014 leon. All rights reserved.
//

#import "ChangePasswordBoard.h"

#pragma mark -

@interface ChangePasswordBoard()
{
	//<#@private var#>
}
@end

@implementation ChangePasswordBoard
DEF_SIGNAL(SEND)

- (void)load
{
}

- (void)unload
{
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    self.title = @"修改密码";
    [self showNaviBar];
    [self showBarButton:BeeUINavigationBar.LEFT image:IMAGESTRING(@"navi_back")];
    //self.view.backgroundColor = RGB(246, 246, 246);
    [self loadContent];
}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
    if (!btnSend) {
        btnSend = [BaseButton spawn];
        btnSend.frame = CGRectMake(260, 0, 60, 44);
        [btnSend setTitle:@"发送" forState:UIControlStateNormal];
        [btnSend setTitleColor:kBlueColor forState:UIControlStateNormal];
        [btnSend addSignal:ChangePasswordBoard.SEND forControlEvents:UIControlEventTouchUpInside];
    }
    [self.navigationController.navigationBar addSubview:btnSend];
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
    [btnSend removeFromSuperview];
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
    [self.stack popBoardAnimated:YES];
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}

ON_SIGNAL2(ChangePasswordBoard, signal){
    if ([signal is:ChangePasswordBoard.SEND]) {
        for (int i = 0; i < 3; i++) {
            if (field[i].text.length==0) {
                [[BeeUITipsCenter sharedInstance] presentMessageTips:@"请输入完整信息"];
                return;
            }
        }
        if (![field[1].text isEqualToString:field[2].text]) {
            [[BeeUITipsCenter sharedInstance] presentMessageTips:@"两次密码输入不一致"];
            return;
        }
        
        [self POST:[NSString stringWithFormat:@"%@/apiuser/changepwd",kBaseUrl]].PARAM(@"username",kUserInfo[@"username"]).PARAM(@"password",field[0].text).PARAM(@"newpassword",field[1].text);
    }
}

- (void)handleRequest:(BeeRequest *)request
{
    if(request.failed)
    {
        [[BeeUITipsCenter sharedInstance] dismissTips];
        NETWORK_ERROR
    }
    else if (request.succeed)
    {
        id json = [request.responseString mutableObjectFromJSONString];
        NSLog(@"%@",json);
        switch ([json[@"status"] intValue]) {
            case 1:
            {
                [BeeUIAlertView showMessage:@"修改密码成功" cancelTitle:@"确定"];
                [self.stack popBoardAnimated:YES];
            }
                break;
        }
    }
}

- (void)loadContent{
    self.view.backgroundColor = RGB(246, 246, 246);
    
    myScrollView = [BeeUIScrollView spawn];
    myScrollView.frame = CGRectMake(0, 0, self.viewWidth, self.viewHeight-(IOS7_OR_LATER?64:44));
    [self.view addSubview:myScrollView];
    
    NSArray *titles = @[@"  旧密码",@"  新密码",@"  重复密码"];
    NSArray *holder = @[@"请输入旧密码",@"至少6位",@"请重复一次密码"];
    
    for (int i = 0; i < 3; i++) {
        UIView *line = [self createLine];
        line.frame = CGRectMake(0, 25+44*i, self.viewWidth, line.height);
        [myScrollView addSubview:line];
        
        BaseLabel *lb = [BaseLabel initLabel:titles[i] font:FONT(14) color:RGB(95, 95, 95) textAlignment:NSTextAlignmentLeft];
        lb.frame = CGRectMake(0, 0, 84, 44);
        
        field[i] = [BeeUITextField spawn];
        field[i].backgroundColor = [UIColor whiteColor];
        field[i].frame = CGRectMake(0, line.bottom, self.viewWidth, 44);
        field[i].contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        field[i].secureTextEntry = YES;
        field[i].font = FONT(14);
        field[i].placeholder = holder[i];
        field[i].leftView = lb;
        field[i].leftViewMode = UITextFieldViewModeAlways;
        field[i].clearButtonMode = UITextFieldViewModeWhileEditing;
        [myScrollView addSubview:field[i]];
    }
    
    UIView *line = [self createLine];
    line.frame = CGRectMake(0, field[2].bottom, self.viewWidth, line.height);
    [myScrollView addSubview:line];
}

- (UIView *)createLine{
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewWidth, 0.5)];
    line.backgroundColor = RGB(187, 187, 187);
    return line;
}

@end
